const BehaviorAll = require("../../../behaviors/behavior_all.js");
const ServiceProduct = require("../../../service/service_product.js");
const ServicePosition = require("../../../service/service_position.js");
const ServiceUser = require("../../../service/service_user.js");

module.exports = {
  /**
   * behaviors
   */
  behaviors: [BehaviorAll],
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    currentTab: 1,
    /**
     * 产品滚动图
     * 数据格式：
        [
          {id:1, url: "https://img1.360buyimg.com/pop/jfs/t25117/290/2321508907/55387/35d9d12/5bc844a6Nc2d978ab.jpg"},
          {id: 2, url: "https://img20.360buyimg.com/da/jfs/t18685/238/1455346844/110119/f5bc9082/5acb3b0eN79be7f6e.jpg"},
          {id: 3, url: "https://m.360buyimg.com/babel/jfs/t25696/263/2271754861/46876/599f49bf/5bc73b97N53c4b287.jpg"}
        ]
     */
    slideImgs: [],
    /**
     * 产品
     * 数据格式：
        {
          id: 1,
          name: "法国进口红酒拉斐天使庄园干红葡萄酒红酒整箱礼盒装750ml*6",
          price: 100,
          detail: '<ol class=" list-paddingleft-2" style="list-style-type: decimal;"><li><p><span style="text-decoration: underline; color: rgb(255, 0, 0);"><em><strong>abc<img src="http://img.baidu.com/hi/jx2/j_0015.gif" /></strong></em></span></p></li ><li><p><span style="color: rgb(255, 0, 0); border: 1px solid rgb(0, 0, 0); text-decoration: line-through; background-color: rgb(255, 255, 0);"><em><strong>asdfbb</strong></em></span></p></li></ol >'
        }
     */
    product: null,
    /**
     * 代理区域相关
     */
    proxyArea_data: [],
    proxyArea_data_list: [],
    selectedDropdownItem1: null,
    selectedDropdownItem2: null,
    /**
     * 购物车相关
     */
    car_amount: 0,
    triggerRetrieveData: false
  },

  methods: {
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      /** 获取数据 */
      const _this = this;
      const id = options.id;
      //
      ServiceProduct.listSlideImg(id, function (data) {
        _this.setData({ slideImgs: data.records })
      });
      //
      ServiceProduct.findById(id, function (data) {
        _this.setData({ product: data });
      });
      ServiceUser.getInfo(function (data) {
        _this.setData({
          userInfo: data
        })
      });
    },
    /**
    * onShow
    */
    onShow: function (e) {
      const _this = this;
      _this.setData({ triggerRetrieveData: true });
    },
    /**
    * 获取数据
    */
    car_onRetrieve: function (e) {
      const _this = this;
      _this.setData({
        car_amount: e.detail.amount
      })
    },
    /**
     * 添加到购物车
     */
    onAdd: function (e) {
      const _this = this;
      const productId = _this.data.product.id;
      ServiceProduct.findById(productId, function (data) {
        _this.setData({
          triggerAddData: {
            id: data.id,
            name: data.name,
            value: data.price,
            amount: data.amount,
            imgUrl: data.imgUrl,
            amount: 1,
            selected: true
          }
        })
      });
    },
    /**
     * 从购物车移除
     */
    onRemove: function (e) {
      const _this = this;
      const productId = _this.data.product.id;
      _this.setData({
        triggerRemoveData: { id: productId }
      })
    },
    /**
     * 数据改变
     */
    car_onChange: function (e) {
      const _this = this;
      _this.setData({
        car_amount: e.detail.amount,
        allDataMap: e.detail.allDataMap
      })
    },
    /**
     * 点击Tap标签
     */
    ontap: function (e) {
      const _this = this;
      const tab = e.currentTarget.dataset.tab;
      // tab改变则处理
      if (_this.data.currentTab !== tab) {
        _this.setData({
          currentTab: tab
        });
      }
      // 代理区域
      if (tab == 2) {
        this.listFirstLevel();
      }
    },

    /**
     * 打开选择区域弹出框，列出省份
     */
    listFirstLevel: function () {
      const _this = this;
      ServicePosition.provinceList(data => {
        this.setData({
          proxyArea_data: [
            {
              id: 1, name: "选择省份",
              items: data.records
            },
            {
              id: 2, name: "选择地级市",
              items: []
            }
          ]
        })
      });
    },

    /**
     * 选择具体省份
     */
    onDropdownItemTap: function (e) {
      const _this = this;
      const selectedDropdown = e.detail.selectedDropdown;
      const selectedDropdownItem = e.detail.selectedDropdownItem;
      const id = selectedDropdownItem.id;
      if (selectedDropdown.id == 1) {
        // 获取数据
        ServicePosition.cityList(id, data => {
          _this.data.proxyArea_data[0].name = selectedDropdownItem.name;
          _this.data.proxyArea_data[1] = {
            id: 2, name: "选择地级市",
            items: data.records
          };
          _this.setData({
            proxyArea_data: _this.data.proxyArea_data,
            proxyArea_data_list: [],
            selectedDropdownItem1: selectedDropdownItem
          });
        })
      } else if (selectedDropdown.id == 2) {
        // 获取数据
        ServicePosition.townList(id, _this.data.product.id, data => {
          _this.data.proxyArea_data[1].name = selectedDropdownItem.name;
          _this.setData({
            proxyArea_data_list: data.records,
            proxyArea_data: _this.data.proxyArea_data,
            selectedDropdownItem2: selectedDropdownItem
          });
        })
      }
    },

    /**
     * 
     */
    onSubmitAreaProxy: function (e) {
      const _this = this;
      const town = e.currentTarget.dataset.town;
      let fixAddress = _this.data.selectedDropdownItem1.name + "/" + _this.data.selectedDropdownItem2.name + "/" + town.name;
      // 跳转到提交订单页面
      wx.navigateTo({
        url: '../orderSubmit/orderSubmit?townId=' + town.id + "&productId=" + _this.data.product.id + "&fixAddress=" + fixAddress
      });
    },

    showImg: function (event) {
      const src = event.currentTarget.dataset.src;
      wx.previewImage({
        current: src, // 当前显示图片的http链接
        urls: [src] // 需要预览的图片http链接列表
      })
    }
  }

};